const Service = require('egg').Service;
const path = require('path')
const fs = require('fs')
// 目前系统支持的类型
const mimes = {
    'json': 'application/json',
    'pdf': 'application/pdf',
    'swf': 'application/x-shockwave-flash',
    'xls': 'application/msexcel',
    'xlsx': 'application/msexcel',
    'ppt': 'application/mspowerpoint',
    'doc': 'application/msword',
    'exe': 'application/octet-stream',
    'zip': 'application/zip',
    'txt': 'text/plain',
    'css': 'text/css',
    'less': 'text/css',
    'html': 'text/html',
    'xml': 'text/xml',
    'js': 'text/javascript',
    'gif': 'image/gif',
    'ico': 'image/x-icon',
    'jpeg': 'image/jpeg',
    'jpg': 'image/jpeg',
    'png': 'image/png',
    'svg': 'image/svg+xml',
    'tiff': 'image/tiff',
    'wav': 'audio/x-wav',
    'wma': 'audio/x-ms-wma',
    'mp3': 'audio/mpeg',
    'wmv': 'video/x-ms-wmv',
    'mp4': 'video/mp4',
    'ogv': 'video/ogg',
    'flv': 'video/x-flv',
    'avi': 'video/x-msvideo',
    'rmvb': 'application/vnd.rn-realmedia',
    'rm': 'application/vnd.rn-realmedia'
}
const imageTypes = ['gif', 'ico', 'jpg', 'jpeg', 'png', 'svg']
const audioTypes = ['mp3', 'wav', 'wma']
const videoTypes = ['wmv', 'mp4', 'ogv', 'flv', 'avi', 'rmvb', 'rm']
const fileTypes = ['json', 'pdf', 'swf', 'xls', 'xlsx', 'ppt', 'doc', 'exe', 'txt', 'zip']

class FileService extends Service {
    // 解析资源类型
    parseMime (url) {
        let extName = path.extname(url)
        extName = extName ? extName.slice(1) : 'unknown'
        return mimes[ extName ]
    }

    // 遍历并处理文件夹内容
    walk(reqPath) {
        // 文件夹列表
        let dirList = []
        // 文件列表
        let fileList = []
        let files = fs.readdirSync(reqPath)
        files.forEach(i => {
            // console.log(i)
            let itemArr = i.split('.')
            let itemMime = (itemArr.length > 1) ? itemArr[ itemArr.length - 1 ] : 'undefined'
            //0-文件夹，1-图片，2-文件，3-音频，4-视频，5-其他
            if (typeof mimes[ itemMime ] === 'undefined') {
                dirList.push({ name: i, type: 0 })
            } else if(imageTypes.indexOf(itemMime) !== -1) {
                fileList.push({ name: i, type: 1 })
            } else if(fileTypes.indexOf(itemMime) !== -1) {
                fileList.push({ name: i, type: 2 })
            } else if(audioTypes.indexOf(itemMime) !== -1) {
                fileList.push({ name: i, type: 3 })
            } else if(videoTypes.indexOf(itemMime) !== -1) {
                fileList.push({ name: i, type: 4 })
            } else {
                fileList.push({ name: i, type: 5 })
            }
        })

        const result = dirList.concat(fileList)
        return result
    }

    // 返回文件夹内容
    dir(directory, reqPath) {
        let contentList = this.walk(reqPath)
        let list = []
        for(let [ key, value ] of contentList.entries()) {
            list.push({index: key, name: value.name, type: value.type, link: directory + value.name})
        }
        return list
    }

    content(directory, fullStaticPath) {
        const reqPath = path.join(fullStaticPath, '/public/' + directory)
        const isExist = fs.existsSync(reqPath)
        let result = ''
        if(!isExist) {
            result = '请求路径 ' + reqPath + ' 不存在！'
        } else {
            let stats = fs.statSync(reqPath)
            if(stats.isFile()) {
                result = fs.readFileSync(filePath, 'binary')
            } else if(stats.isDirectory()) {
                result = this.dir('/public'+directory+'/', reqPath)
            }
        }
        return result
    }
}

module.exports = FileService;